package com.tsinjie.metadata.java.model;

import com.tsinjie.metadata.java.model.hibernate.ManyToMany;
import com.tsinjie.metadata.java.model.hibernate.ManyToOne;
import com.tsinjie.metadata.java.model.hibernate.OneToMany;
import com.tsinjie.metadata.java.model.hibernate.OneToOne;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuXunming on 2015-10-14.
 * java类
 */
public class JavaClass implements Serializable, Cloneable {
    public static final Integer LOGIN_CLASS = 1;
    /**
     * 导入包
     */
    private List<String> importPackages;
    /**
     * 所在包
     */
    private String packageName;
    /**
     * 类名
     */
    private String className;
    /**
     * 对应表名
     */
    private String tableName;
    /**
     * 所属模块
     */
    private String module;
    /**
     * 类注释
     */
    private String remark;
    /**
     * 是否为登录类
     */
    private Integer loginClass;
    /**
     * 主属性列表
     */
    private List<JavaField> identifies;
    /**
     * 外来属性
     */
    private List<JavaField> foreignFields;
    /**
     * 普通字段列表
     */
    private List<JavaField> normalFields;
    /*hibernate 关系*/
    /**
     * hibernate关系：一对一作为副表
     */
    private OneToOne oneToOne;
    /**
     * hibernate关系：一对一作为主表
     */
    private List<OneToOne> oneToOneList;
    /**
     * hibernate关系：多对一
     */
    private List<ManyToOne> manyToOneList;
    /**
     * hibernate关系：一对多
     */
    private List<OneToMany> oneToManyList;
    /**
     * hibernate关系：多对多
     */
    private List<ManyToMany> manyToManyList;

    public List<String> getImportPackages() {
        return importPackages;
    }

    public void setImportPackages(List<String> importPackages) {
        this.importPackages = importPackages;
    }

    /**
     * 添加导入包
     *
     * @param packageName
     */
    public void addImportPackage(String packageName) {
        if (importPackages == null) {
            importPackages = new ArrayList<String>();
        }
        if (!importPackages.contains(packageName)) {
            importPackages.add(packageName);
        }
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public Integer getLoginClass() {
        return loginClass;
    }

    public void setLoginClass(Integer loginClass) {
        this.loginClass = loginClass;
    }

    public List<JavaField> getIdentifies() {
        return identifies;
    }

    public void setIdentifies(List<JavaField> identifies) {
        this.identifies = identifies;
    }

    /**
     * 添加主属性
     *
     * @param identify
     */
    public void addIdentify(JavaField identify) {
        if (identifies == null) {
            identifies = new ArrayList<JavaField>();
        }
        identifies.add(identify);
    }

    /**
     * 获取主属性字段（就一般情况而言，类只有一个主属性，不是吗？）
     *
     * @return
     */
    public JavaField getIdentify() {
        return identifies.get(0);
    }

    public List<JavaField> getNormalFields() {
        return normalFields;
    }

    public void setNormalFields(List<JavaField> normalFields) {
        this.normalFields = normalFields;
    }

    /**
     * 添加普通字段
     *
     * @param javaField
     */
    public void addNormalField(JavaField javaField) {
        if (normalFields == null) {
            normalFields = new ArrayList<JavaField>();
        }
        normalFields.add(javaField);
    }

    public List<JavaField> getForeignFields() {
        return foreignFields;
    }

    public void setForeignFields(List<JavaField> foreignFields) {
        this.foreignFields = foreignFields;
    }

    /**
     * 添加外来字段
     *
     * @param javaField
     */
    public void addForeignField(JavaField javaField) {
        if (foreignFields == null) {
            foreignFields = new ArrayList<JavaField>();
        }
        foreignFields.add(javaField);
    }

    /**
     * 获取全部字段
     *
     * @return
     */
    public List<JavaField> getFields() {
        List<JavaField> fields = new ArrayList<JavaField>();
        if (identifies != null)
            fields.addAll(identifies);
        if (foreignFields != null)
            fields.addAll(foreignFields);
        if (normalFields != null)
            fields.addAll(normalFields);
        return fields;
    }

    public OneToOne getOneToOne() {
        return oneToOne;
    }

    public void setOneToOne(OneToOne oneToOne) {
        this.oneToOne = oneToOne;
    }

    public List<OneToOne> getOneToOneList() {
        return oneToOneList;
    }

    public void setOneToOneList(List<OneToOne> oneToOneList) {
        this.oneToOneList = oneToOneList;
    }

    /**
     * 添加一对一（作为主表时）
     *
     * @param oneToOne
     */
    public void addOneToOne(OneToOne oneToOne) {
        if (oneToOneList == null) {
            oneToOneList = new ArrayList<OneToOne>();
        }
        oneToOneList.add(oneToOne);
    }

    public List<ManyToOne> getManyToOneList() {
        return manyToOneList;
    }

    public void setManyToOneList(List<ManyToOne> manyToOneList) {
        this.manyToOneList = manyToOneList;
    }

    /**
     * 添加多对一
     *
     * @param manyToOne
     */
    public void addManyToOne(ManyToOne manyToOne) {
        if (manyToOneList == null) {
            manyToOneList = new ArrayList<ManyToOne>();
        }
        manyToOneList.add(manyToOne);
    }

    public List<OneToMany> getOneToManyList() {
        return oneToManyList;
    }

    public void setOneToManyList(List<OneToMany> oneToManyList) {
        this.oneToManyList = oneToManyList;
    }

    /**
     * 添加一对多
     *
     * @param oneToMany
     */
    public void addOneToMany(OneToMany oneToMany) {
        if (oneToManyList == null) {
            oneToManyList = new ArrayList<OneToMany>();
        }
        oneToManyList.add(oneToMany);
    }

    public List<ManyToMany> getManyToManyList() {
        return manyToManyList;
    }

    public void setManyToManyList(List<ManyToMany> manyToManyList) {
        this.manyToManyList = manyToManyList;
    }

    /**
     * 添加多对多
     *
     * @param manyToMany
     */
    public void addManyToMany(ManyToMany manyToMany) {
        if (manyToManyList == null) {
            manyToManyList = new ArrayList<ManyToMany>();
        }
        manyToManyList.add(manyToMany);
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    /**
     * 浅拷贝
     */
    @Override
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }

    @Override
    public String toString() {
        return "JavaClass{" +
                "importPackages=" + importPackages +
                ", packageName='" + packageName + '\'' +
                ", className='" + className + '\'' +
                ", module='" + module + '\'' +
                ", identifies=" + identifies +
                ", normalFields=" + normalFields +
                ", tableName='" + tableName + '\'' +
                ", remark='" + remark + '\'' +
                ", oneToOne=" + oneToOne +
                ", oneToOneList=" + oneToOneList +
                ", manyToOneList=" + manyToOneList +
                ", oneToManyList=" + oneToManyList +
                ", manyToManyList=" + manyToManyList +
                '}';
    }
}
